package com.cwj.springboot_study.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cwj.springboot_study.mapper.PersonMapper;
import com.cwj.springboot_study.model.Person;
import com.cwj.springboot_study.repositories.PersonRepository;
import com.cwj.springboot_study.service.PersonService;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.utility.RandomString;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.web.context.support.WebApplicationContextUtils;

import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * @author wujie.chen
 */
@Service
@Slf4j
public class PersonServiceImpl extends ServiceImpl<PersonMapper, Person> implements PersonService {

    @Autowired
    private PersonRepository personRepository;
    @Autowired
    private ApplicationContext applicationContext;


    @Override
    @Transactional
    public int m1() {
        Optional<Person> byId = personRepository.findById(1);
        if (byId.isPresent()) {
            Person person = byId.get();
            String address = "北京" + RandomString.make();
            System.out.println("m1:updated" + address);
            person.setAddress(address);
            personRepository.save(person);
            new Thread(){
                @Override
                public void run() {
                    ((PersonService)AopContext.currentProxy()).m2();
                }
            }.start();
            //            applicationContext.getBean(PersonService.class).m2();
            //                        m2();
            return 1;
        }
        return 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public int m2() {
        Optional<Person> byId = personRepository.findById(1);
        if (byId.isPresent()) {
            Person person = byId.get();
            String address = "上海" + RandomString.make();
            System.out.println("m2:updated" + address);
            person.setAddress(address);
            personRepository.save(person);
            return 1;
        }
        return 0;
    }

    @Transactional
    @Override
    public Person add(Person person) {
        String username = person.getUsername();
        boolean has = personRepository.existsByUsernameAndDeletedIsFalse(username);
        if(has){
            throw new RuntimeException("username: " + username + " is exists");
        }
        person.setDeleted(false);
        log.info("add person");
        Person save = personRepository.save(person);
        throw new RuntimeException("");
    }

    @Override
    public void login(String username, String password) {
        Person person= personRepository.findByUsername(username);
        if(person == null){
            throw new RuntimeException("用户名或密码不正确");
        }
        String encodedPassword = person.getPassword();
        System.out.println("login success!");
    }

    @Override
    @Transactional
    public void register(String username, String password) {
        boolean has = personRepository.existsByUsernameAndDeletedIsFalse(username);
        if(has){
            throw new RuntimeException("username: " + username + " is exists");
        }
        Person person = new Person();
        person.setUsername(username);
        person.setPassword(password);
        person.setDeleted(false);
        personRepository.save(person);
    }

    @Override
    @Transactional
    public void testTS(Person person) {
        personRepository.save(person);
        throw new RuntimeException();
    }

}
